<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { Search, Star, Back } from '@element-plus/icons-vue'
import { foundation } from '@/api'

const route = useRoute()
const router = useRouter()

// 返回上一页
const goBack = () => {
  router.back()
}

// 搜索关键词
const searchKeyword = ref('')

// 排序选项
const sortOptions = [
  { label: '综合排序', value: 'default' },
  { label: '销量优先', value: 'sales' },
  { label: '价格从低到高', value: 'price-asc' },
  { label: '价格从高到低', value: 'price-desc' },
  { label: '评分最高', value: 'rating' }
]

// 价格区间
const priceRange = ref([0, 5000])

// 品牌选项
const brands = ref([
  { label: 'Nike', value: 'Nike' },
  { label: 'Adidas', value: 'Adidas' },
  { label: 'New Balance', value: 'New Balance' },
  { label: 'Converse', value: 'Converse' },
  { label: 'Vans', value: 'Vans' },
  { label: 'Puma', value: 'Puma' }
])

// 选中的品牌
const selectedBrands = ref([])

// 搜索结果
const searchResults = ref([])

// 加载搜索结果
const loadSearchResults = () => {
  // 模拟数据
  foundation({
    name: searchKeyword.value,
    priceRange: priceRange.value
  }).then(res => {
    searchResults.value = res.data.data
  })


  // searchResults.value = [
  //   {
  //     id: 1,
  //     name: 'Air Jordan 1',
  //     brand: 'Nike',
  //     price: 1299,
  //     originalPrice: 1499,
  //     image: 'https://via.placeholder.com/300x300?text=AJ1',
  //     rating: 4.8,
  //     sales: 1200
  //   },
  //   {
  //     id: 2,
  //     name: 'Ultra Boost',
  //     brand: 'Adidas',
  //     price: 1199,
  //     originalPrice: 1399,
  //     image: 'https://via.placeholder.com/300x300?text=UltraBoost',
  //     rating: 4.7,
  //     sales: 980
  //   }
  // ]
}

// 处理搜索
const handleSearch = () => {
  loadSearchResults()
}

// 处理排序变化
const handleSortChange = (value) => {
  currentSort.value = value
  loadSearchResults()
}

// 处理价格区间变化
const handlePriceRangeChange = () => {
  loadSearchResults()
}

// 处理品牌选择变化
const handleBrandChange = () => {
  loadSearchResults()
}

onMounted(() => {
  // 从路由参数获取搜索关键词
  const keyword = route.query.keyword
  if (keyword) {
    searchKeyword.value = keyword
    loadSearchResults()
  }
})
</script>

<template>
  <div class="search-result">
    <div class="container">
      <!-- 返回按钮 -->
      <div class="back-button">
        <el-button @click="goBack" :icon="Back" text>返回上一页</el-button>
      </div>

      <!-- 搜索框 -->
      <div class="search-bar">
        <el-input
          v-model="searchKeyword"
          placeholder="搜索你喜欢的球鞋"
          :prefix-icon="Search"
          @keyup.enter="handleSearch"
        >
          <template #append>
            <el-button @click="handleSearch">搜索</el-button>
          </template>
        </el-input>
      </div>

      <!-- 筛选区域 -->
      <div class="filter-section">
        <el-card>
          <!-- 品牌筛选 -->
          <div class="filter-group">
            <h3>品牌</h3>
            <el-checkbox-group v-model="selectedBrands" @change="handleBrandChange">
              <el-checkbox
                v-for="brand in brands"
                :key="brand.value"
                :label="brand.value"
              >
                {{ brand.label }}
              </el-checkbox>
            </el-checkbox-group>
          </div>

          <!-- 价格区间 -->
          <div class="filter-group">
            <h3>价格区间</h3>
            <el-slider
              v-model="priceRange"
              range
              :min="0"
              :max="5000"
              :step="100"
              @change="handlePriceRangeChange"
            />
            <div class="price-range-text">
              ¥{{ priceRange[0] }} - ¥{{ priceRange[1] }}
            </div>
          </div>
        </el-card>
      </div>

      <!-- 排序工具栏 -->
      <!--
      <div class="sort-toolbar">
        <el-radio-group v-model="currentSort" @change="handleSortChange">
          <el-radio-button
            v-for="option in sortOptions"
            :key="option.value"
            :label="option.value"
          >
            {{ option.label }}
          </el-radio-button>
        </el-radio-group>
      </div>
      -->

      <!-- 搜索结果 -->
      <div class="search-results">
        <el-row :gutter="20">
          <el-col
            v-for="product in searchResults"
            :key="product.id"
            :xs="24"
            :sm="12"
            :md="8"
            :lg="6"
            class="product-col"
          >
            <el-card class="product-card" :body-style="{ padding: '0px' }">
              <div class="product-img-container">
                <img :src="product.image" class="product-image" />
              </div>
              
              <div class="product-info">
                <h3 class="product-name">{{ product.name }}</h3>
                <p class="product-brand">{{ product.brand }}</p>
                
                <div class="product-price-row">
                  <span class="product-price">¥{{ product.price }}</span>
                  <span class="product-original-price">¥{{ product.originalPrice }}</span>
                </div>
                
                <div class="product-meta">
                  <span class="product-rating">
                    <el-icon><Star /></el-icon> {{ product.rating }}
                  </span>
                  <span class="product-sales">销量 {{ product.sales }}</span>
                </div>
                
                <el-button type="primary" class="add-to-cart-btn">
                  加入购物车
                </el-button>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<style scoped>
.search-result {
  padding: 24px;
  min-height: 100vh;
  background-color: #f5f5f5;
}

.container {
  max-width: 1400px;
  margin: 0 auto;
}

.search-bar {
  margin-bottom: 24px;
}

.filter-section {
  margin-bottom: 24px;
}

.filter-group {
  margin-bottom: 20px;
}

.filter-group h3 {
  font-size: 16px;
  margin-bottom: 12px;
  color: #333;
}

.price-range-text {
  margin-top: 8px;
  color: #666;
}

.sort-toolbar {
  margin-bottom: 24px;
}

.product-col {
  margin-bottom: 20px;
}

.product-card {
  transition: all 0.3s ease;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.product-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
}

.product-img-container {
  height: 240px;
  overflow: hidden;
}

.product-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s ease;
}

.product-card:hover .product-image {
  transform: scale(1.05);
}

.product-info {
  padding: 15px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.product-name {
  font-size: 18px;
  margin: 0 0 8px;
  font-weight: 500;
  color: #333;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.product-brand {
  font-size: 14px;
  color: #666;
  margin: 0 0 10px;
}

.product-price-row {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.product-price {
  font-size: 20px;
  font-weight: bold;
  color: #f56c6c;
  margin-right: 10px;
}

.product-original-price {
  font-size: 14px;
  color: #999;
  text-decoration: line-through;
}

.product-meta {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
  font-size: 13px;
  color: #666;
}

.product-rating {
  display: flex;
  align-items: center;
}

.product-rating .el-icon {
  color: #ff9900;
  margin-right: 4px;
}

.add-to-cart-btn {
  margin-top: auto;
  width: 100%;
}

@media (max-width: 768px) {
  .sort-toolbar {
    overflow-x: auto;
    white-space: nowrap;
    padding-bottom: 10px;
  }
}

.back-button {
  margin-bottom: 16px;
}
</style>